package com.song.newdateapi;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class Java8TesterDate {
	public static void main(String[] args) {
		// 构造对象--我们看看从不同方式所构造的对象实例
		LocalDateTime timePoint = LocalDateTime.now(); // 当前的日期和时间。
		System.out.println("当前的日期和时间:--------> " + timePoint);
		System.out.println("基于数值构造:------->" + LocalDate.of(2012, Month.DECEMBER, 12));
		System.out.println("1970中间:------->" + LocalDate.ofEpochDay(2));
		System.out.println("今天我坐火车回家:------->" + LocalTime.of(17, 18));
		System.out.println("从字符串构造:------->" + LocalTime.parse("10:15:30"));
		// Instant——它代表的是时间戳
		Instant ins = Instant.now();
		System.out.println("Instant——它代表的是时间戳:--------"+ins);

		// 从对象中获取时间数值
		LocalDate theDate = timePoint.toLocalDate();
		Month month = timePoint.getMonth();
		int day = timePoint.getDayOfMonth();
		int second = timePoint.getSecond();
		System.out.println("theDate--------->" + theDate + ",month:" + month + ",day:" + day + ",second:" + second);

		// 由于值不可变的原因，需要使用with方法返回新的对象，而不是使用set方法去变更原来的时间。
		// Set the value, returning a new object
		LocalDateTime thePast = timePoint.withDayOfMonth(2).withYear(2010);
		System.out.println("thePast:-------->"+thePast);

		/*
		 * You can use direct manipulation methods, or pass a value and field pair
		 */
		LocalDateTime yetAnother = thePast.plusWeeks(2).plus(3, ChronoUnit.WEEKS);
		System.out.println("yetAnother:---------->"+yetAnother);
		
//		截断
//		新的API允许你从完整的日期和时间对象中进行类型截断。使用truncatedTo可以达到截断的目的：
		LocalDateTime truncatedTime = timePoint.truncatedTo(ChronoUnit.SECONDS);
		System.out.println("truncatedTime:--------->"+truncatedTime);
		
		
		// 你可以指定时区的ID来构造ZonedDateTime对象。
		ZoneId id = ZoneId.of("Europe/Paris");
		ZonedDateTime zoned = ZonedDateTime.of(timePoint, id);
		System.out.println("zoned------->"+zoned);
		
		// 时区调整：
		ZoneOffset offset = ZoneOffset.of("+02:00");
		System.out.println(offset);
		OffsetDateTime odt = OffsetDateTime.of(timePoint, offset);
		System.out.println(odt+"----时区调整----"+timePoint);
		
//		TimeZone对象
		System.out.println("TimeZone对象: ----------"+ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Europe/Paris]"));
		
		// Period 代表一个周期性时间的值，例如“3个月又1天”，表示一个和timeline的距离。
		// 三年, 两月, 一天
		Period period = Period.of(3, 2, 1);
		System.out.println("period----------"+period);
		
		// You can modify the values of dates using periods
		LocalDateTime newDate = timePoint.plus(period); // +
		LocalDateTime newDateTime = timePoint.minus(period);// -
		// Components of a Period are represented by ChronoUnit values
		System.out.println("newDate--------"+newDate+",newDateTime------"+newDateTime);
		
		
		// 一个持续3毫秒和5纳秒的时间。
		Duration duration = Duration.ofSeconds(3, 5);
		Duration oneDay = Duration.between(thePast, timePoint);
		
		System.out.println("duration--------"+duration+",oneDay---------"+oneDay);
		
		
		// 对日期进行解析/格式化
		LocalDate formatDate = LocalDate.parse("20180122", DateTimeFormatter.BASIC_ISO_DATE);
		System.out.printf("格式化后的时间是 %s.", formatDate);
		
		
	}
}
